package com.gaswell.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaswell.entity.Qaa02;
import com.gaswell.mapper.Qaa02Mapper;
import com.gaswell.pojo.ALLQj;
import com.gaswell.service.ALLQjService;
import com.gaswell.service.IQaa02Service;
import com.gaswell.service.QjService;
import com.gaswell.vo.QjVo;
import com.gaswell.vo.Result;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 钻井地质信息 服务实现类
 * </p>
 *
 * @author Lei Wang
 * @since 2022-05-24
 */
@Service
@DS("oracle")
public class Qaa02ServiceImpl extends ServiceImpl<Qaa02Mapper, Qaa02> implements IQaa02Service {
    @Autowired
    private Qaa02Mapper qaa02Mapper;
    @Autowired
    ALLQjService allQjService;
    @Autowired
    QjService qjService;

    @Override
    public Result insertOne(Qaa02 qaa02) {
        qaa02Mapper.insert(qaa02);
        return Result.success(qaa02);
    }

    @Override
    public Result updateOne(Qaa02 qaa02) {
        qaa02Mapper.updateById(qaa02);
        return Result.success(qaa02);
    }

    @Override
    public Result selectProperties(Qaa02 qaa02) {
        Class cls = qaa02.getClass();
        Field[] fields = cls.getDeclaredFields();
        QueryWrapper<Qaa02> queryWrapper = new QueryWrapper<>();
        for (Field field : fields) {
            String fname = field.getName();
            String type = field.getGenericType().toString();

            field.setAccessible(true);
            try {

                if (type.equals("class java.lang.String") && StringUtils.isNotBlank(field.get(qaa02).toString())) {
                    if (fname.equals("rq")) {

                        String[] dates = field.get(qaa02).toString().split(",");
                        String down = dates[0];
                        String up = dates[1];
                        queryWrapper.ge(fname, down);
                        queryWrapper.le(fname, up);
                    } else {
                        queryWrapper.eq(fname, field.get(qaa02).toString());
                    }
                }
                if (type.equals("java.math.BigDecimal") && Double.valueOf(field.get(qaa02).toString()) != 0) {
                    queryWrapper.eq(fname, Double.valueOf(field.get(qaa02).toString()));
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
//        Page<Qaa02> page=new Page<>(current,size);
//        qaa02Mapper.selectPage(page, queryWrapper);
//        return new Result(true,200,"success",page.getRecords(),(int)page.getTotal(),(int)page.getPages());
        List<Qaa02> list = qaa02Mapper.selectList(queryWrapper);
        return Result.success(list);
    }

    @Override
    public Result selectAll(int current, int size, String jh) {
        Page<Qaa02> page = new Page<>(current, size);
        LambdaQueryWrapper<Qaa02> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(jh)) {
//            在视图中 查出对应的中文编号和英文编号
            LambdaQueryWrapper<ALLQj> allQjLambdaQueryWrapper = new LambdaQueryWrapper<>();
            allQjLambdaQueryWrapper.eq(ALLQj::getYwbh, jh).or().eq(ALLQj::getZwbh, jh);
            List<ALLQj> list = allQjService.list(allQjLambdaQueryWrapper);
            if (list != null && list.size() > 0) {
                lambdaQueryWrapper.eq(Qaa02::getJh, list.get(0).getYwbh())
                        .or().eq(Qaa02::getJh, list.get(0).getZwbh());
            } else {
//               没有查询到数据
                return new Result(true, 200, "success", new LinkedList<>(), 0, 0);
            }
        }
        qaa02Mapper.selectPage(page, lambdaQueryWrapper);
        return new Result(true, 200, "success", page.getRecords(), (int) page.getTotal(), (int) page.getPages());
    }

    @Override
    public Qaa02 selectRGJD(String userName, String jh) {
        LinkedList<String> strings = new LinkedList<>();
        strings.add(jh);
        List<QjVo> jhByJhs = qjService.findJhByJhs(strings);
        List<String> zwbhs = QjVo.getZwbhs(jhByJhs);
        List<String> ywbhs = QjVo.getYwbhs(jhByJhs);


        LambdaQueryWrapper<Qaa02> qaa02LambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (zwbhs.size() == 0 || ywbhs.size() == 0) {
            return null;
        }
        qaa02LambdaQueryWrapper.in(Qaa02::getJh, zwbhs).or().in(Qaa02::getJh, ywbhs);
        List<Qaa02> qaa02s = qaa02Mapper.selectList(qaa02LambdaQueryWrapper);
        if (qaa02s != null && qaa02s.size() > 0)
            return qaa02s.get(0);
        return null;
    }
}
